<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Format\RentHouseFormat;
use App\Http\Requests\CheckRequest;
use App\Http\Requests\SetRentHouseRequest;
use App\Services\RentHouseService;
use Illuminate\Http\Request;

class RentHouseController extends Controller
{
    protected $rentHouseService;

    public function __construct(RentHouseService $rentHouseService)
    {
        $this->rentHouseService = $rentHouseService;
    }

    public function setRentHouseForCheck(SetRentHouseRequest $setRentHouseRequest)
    {
        $format = new RentHouseFormat($setRentHouseRequest->all());
        $userId = $setRentHouseRequest->input('user_id');
        $this->rentHouseService->setRentHouseForCheck($format, $userId);
        return renderSuccessJson();
    }

    public function getList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('address')) {
            $search['address'] = $request->input('address');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }
        $res = $this->rentHouseService->getRentHouseList($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function getAdminList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('address')) {
            $search['address'] = $request->input('address');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }
        if ($request->input('check_status')) {
            $search['check_status'] = $request->input('check_status');
        }
        $res = $this->rentHouseService->getRentHouseAdminList($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        $res = $this->rentHouseService->detail($id);
        return renderSuccessJson($res);
    }

    public function adminDetail(Request $request)
    {
        $id = $request->input('id');
        $res = $this->rentHouseService->detailAdmin($id);
        return renderSuccessJson($res);
    }

    public function check(CheckRequest $checkRequest)
    {
        $id = $checkRequest->input('id');
        $checkStatus = $checkRequest->input('check_status');
        $checkMessage = $checkRequest->input('check_message') ?? '';
        $this->rentHouseService->checkRentHouse($id, $checkStatus, $checkMessage);
        return renderSuccessJson();
    }

    public function setExpireTime(Request $request)
    {
        $id = $request->input('id');
        $expireTime = $request->input('expire_time');
        $this->rentHouseService->setExpireTime($id, $expireTime);
        return renderSuccessJson();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        $this->rentHouseService->delete($id);
        return renderSuccessJson();
    }

    public function getMyRentedHouse(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $userId = $request->input('user_id');
        $res = $this->rentHouseService->getMyRentedHouse($userId, $page, $pageSize);
        return renderSuccessJson($res);
    }

    public function getMyRentHouseDetail(Request $request)
    {
        $id = $request->input('id');
        $userId = $request->input('user_id');
        $res = $this->rentHouseService->getMyRentedHouseDetail($id, $userId);
        return renderSuccessJson($res);
    }

    public function setRentHouseOrder(Request $request)
    {
        $id = $request->input('id');
        $orderNum = $request->input('order_num');
        $this->rentHouseService->setOrder($id, $orderNum);
        return renderSuccessJson();
    }
}
